package fleditor.core.loader
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLRequest;
	import flash.system.LoaderContext;
	
	public class LoaderBase extends BaseLoader
	{
		private var _loader:Loader;
		private var _loaderContext:LoaderContext;
		
		public function LoaderBase(id:int,url:String, autoExecute:Boolean=true, callBack:Function=null, _repeat:int=3, loaderContext:LoaderContext=null)
		{
			_callBack=callBack;
			super(id, url, autoExecute, callBack, _repeat);
			_loaderContext=loaderContext;
			_loader=new Loader();
		}
		
		override protected function __onCompleted(evt:Event):void
		{
			if(_urlLoader.data.length == 0)return;
			
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, __onContentLoadComplete);
			_loader.loadBytes(_urlLoader.data, _loaderContext);
			
			removeEvent();
			_urlLoader.close();
			
			_progressPercent=1;
			_isComplete=true;
			_isLoading=false;
			_isSuccess=true;
		}
		
		protected function __onContentLoadComplete(evt:Event):void
		{
			_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, __onContentLoadComplete);
			if(_loader.parent) _loader.parent.removeChild(_loader);
			_loader.unload();
			_loader=null;
			
			dispatchEvent(new LoaderEvent(LoaderEvent.PROGRESS,this));
			dispatchEvent(new LoaderEvent(LoaderEvent.COMPLETE,this));
			
			if(_callBack!=null) _callBack(this);
		}
		
		override public function dispose():void
		{
			super.dispose();
			
			if(_loader)
			{
				_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, __onContentLoadComplete);
				if(_loader.parent) _loader.parent.removeChild(_loader);
				_loader.unload();
			}
			_loader=null;
		}
	}
}